package com.gyul.findmystock.ui.stock.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gyul.findmystock.R;


public class MacdChartView extends View {
	static final String TAG = "MacdChartView";
	
	
	TextView [] textMacd = new TextView [4];
	
	int chartHeight;
	float chartHeightGap;		
	final int heightGapCount = 5;
	
	float convertFactor;	
	int maxData;
	
	Paint paintMacePlus;
	Paint paintMaceMinus;
		
	public MacdChartView(Context context) {
		super(context);
		
		initializeChartView();
	}
	
	public MacdChartView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		initializeChartView();
	}
	
	@Override
	public void onSizeChanged( int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		Log.d(TAG, "onSizeChanged");
		chartHeight = h;
		chartHeightGap = chartHeight / heightGapCount;
	}
	
	private void initializeChartView() {
		paintMacePlus = new Paint();
		paintMacePlus.setStyle(Paint.Style.FILL);
		paintMacePlus.setColor(Color.RED);
		
		paintMaceMinus = new Paint();
		paintMaceMinus.setStyle(Paint.Style.FILL);
		paintMaceMinus.setColor(Color.BLUE);		
	}
	
	public void initializeChartInfo(View rootView) {
		this.textMacd[0] = (TextView) rootView.findViewById(R.id.text_macd_value_plus1);
		this.textMacd[1] = (TextView) rootView.findViewById(R.id.text_macd_value_plus2);
		this.textMacd[2] = (TextView) rootView.findViewById(R.id.text_macd_value_minus2);
		this.textMacd[3] = (TextView) rootView.findViewById(R.id.text_macd_value_minus1);
		
//		Log.d(TAG, "QuoteList Size : " + quoteList.size() + " StartIndex : " + FragmentStockChart.startIndex);
	}
	
	@Override
	public void onDraw(Canvas canvas) {		
		if ( Chart.macd == null || Chart.initialized == false ) {
			return;
		}
			
		
		setFactor();		
		canvas.drawColor(Chart.chartBackgroundColor);		
		drawMacd(canvas);//		
		drawRange();
		
		if ( Chart.showInfo ) {
			Chart.drawHighlightLine(canvas, chartHeight);
		}
	}
	
	private void drawMacd(Canvas canvas) {
		float osc = 0;
		float oscX = 0;
		float left, right;
				
		for ( int i = Chart.startIndex; i < Chart.startIndex + Chart.drawStockSize && i < Chart.quoteListSize; i++ ) {
			osc = Chart.macd.osc[i];
			
//			Log.d(TAG, "Osc[" + i + "] - " + osc);
			
			left = oscX + Chart.chartWidthGapBlank;
			right = oscX + Chart.chartWidthGap - Chart.chartWidthGapBlank;
			
			if ( osc > 0.0f ) {
				canvas.drawRect(left, getData(osc), right, chartHeight/2, paintMacePlus);
			} else {
				canvas.drawRect(left, chartHeight/2, right, chartHeight/2 + (chartHeight/2 - getData(osc*-1)), paintMaceMinus);
			}
			
			oscX += Chart.chartWidthGap;
		}
	}
	
	private void drawRange() {
		int gap = maxData / 2;
		int data = maxData;
		
		if ( textMacd[0] == null ) {
			return;
		}
		
		for ( int i = 0; i < 4; i++ ) {
			textMacd[i].setText("" + data);
			data -= gap;
			
			if ( i == 1 ) {
				data -= gap;
			}
		}
	}
	
	private float getData(float osc) {		
		return (maxData - osc) * convertFactor;
	}
	
	private void setFactor() {
		setMax();
		convertFactor = (chartHeightGap / (maxData * 0.5f )); 
	}
		
	private void setMax() {
		maxData = Integer.MIN_VALUE;
				
		for ( int i = Chart.startIndex; i < Chart.startIndex + Chart.drawStockSize && i < Chart.quoteListSize; i++ ) {
			if ( Chart.macd.osc[i] < 0.0d ) {
				maxData = (int) Math.max(maxData, Chart.macd.osc[i] * -1);
			} else {
				maxData = (int) Math.max(maxData, Chart.macd.osc[i]);
			}
			
		}
		
		if ( maxData % 1000 != 0 ) {
			maxData += 1000 - ( maxData % 1000);
		}
	}

}
